<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class System extends Common
{
    public function index()
    {
        $channel = input('channel') ?: 1;

        $where['channel'] = $channel;
        
        $data = Db::name('sys_message')->where(['channel' => $channel, 'type' => ['in', [0,30,31]], 'sys_channel' => 1])->order('is_top desc, id desc')->paginate(_getCachePages(), false, ['query' => request()->param()])->each(function($item){
            if ($item['type'] == 31) {
                $item['context'] = json_decode($item['context'], true)['content'];
            }
            return $item;
        });
        $page = $data->render();

        $this->assign('channel', $channel);
        
        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }

    //添加
    public function add()
    {
        $id = input("id");
        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $db = Db::name('sys_message')->insert([
                    'channel' => $post['channel'],
                    'is_top' => $post['is_top'],
                    'type' => 0,
                    'context' => json_encode(['desc' => in_content($post['content'])], JSON_UNESCAPED_UNICODE),
                    'sys_channel' => 1
                ]);

                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $context = json_encode(['desc' => in_content($post['content'])], JSON_UNESCAPED_UNICODE);
                $db = Db::name('sys_message')->where(['id' => $id])->update([
                    'channel' => $post['channel'],
                    'is_top' => $post['is_top'],
                    'context' => $context,
                    'sys_channel' => 1
                ]);

                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('sys_message')->where('id', $id)->find();
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
    public function addGoods()
    {
        $id = input("id");
        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $db = Db::name('sys_message')->insert([
                    'channel' => $post['channel'],
                    'is_top' => $post['is_top'],
                    'age_min' => $post['age_min'],
                    'age_max' => $post['age_max'],
                    'type' => 30,
                    'context' => $post['content'],
                    'sys_channel' => 1
                ]);

                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $db = Db::name('sys_message')->where(['id' => $id])->update([
                    'channel' => $post['channel'],
                    'is_top' => $post['is_top'],
                    'age_min' => $post['age_min'],
                    'age_max' => $post['age_max'],
                    'context' => $post['content'],
                    'sys_channel' => 1
                ]);

                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('sys_message')->where('id', $id)->find();
                if ($name) {
                    $name['content'] = $name['context'];
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
    public function addUrl()
    {
        $id = input("id");
        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $db = Db::name('sys_message')->insert([
                    'channel' => $post['channel'],
                    'is_top' => $post['is_top'],
                    'age_min' => $post['age_min'],
                    'age_max' => $post['age_max'],
                    'type' => 31,
                    'context' => json_encode(['content'=>$post['content'],'url'=>$post['url']], JSON_UNESCAPED_UNICODE),
                    'sys_channel' => 1
                ]);

                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $db = Db::name('sys_message')->where(['id' => $id])->update([
                    'channel' => $post['channel'],
                    'is_top' => $post['is_top'],
                    'age_min' => $post['age_min'],
                    'age_max' => $post['age_max'],
                    'context' => json_encode(['content'=>$post['content'],'url'=>$post['url']], JSON_UNESCAPED_UNICODE),
                    'sys_channel' => 1
                ]);

                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
        } else {
            if ($id) {
                $name = Db::name('sys_message')->where('id', $id)->find();
                if ($name) {
                    $json = json_decode($name['context'], true);
                    $name['content'] = $json['content'];
                    $name['url'] = $json['url'];
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }


    public function watch($id)
    {
        $name = Db::name('sys_message')->find($id);

        $this->assign('name', $name);

        return $this->fetch();
    }

    //删除
    public function del()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('sys_message')->where(['id' => $id])->delete();
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }


    // 批量删除
    public function delAll()
    {
        if (request()->isAjax()) {
            $post = input('post.');

            if (input('?post.delAll')) {
                // halt($post['delAll']);
                $result = Db::name('sys_message')->where(['id' => ['in', $post['delAll']]])->delete();

                $data = $result ? ['ret' => '删除成功'] : ['err' => '删除失败'];
            } else {
                $data = ['err' => '未选中数据'];
            }
        } else {
            $data = ['err' => '提交类型错误'];
        }
        return $data;
    }

    public function wxpay()
    {
        $data = Db::name('config_wxpay')->order('id desc')->paginate(_getCachePages(), false, ['query' => request()->param()])->each(function($item){
            return $item;
        });
        $page = $data->render();

        $this->assign('data', $data);
        $this->assign('page', $page);

        return $this->fetch();
    }

    public function add_wxpay()
    {
        $id = input("id");
        if (request()->isAjax()) {
            $post = input('post.');

            if (!$id) {
                $id = $db = Db::name('config_wxpay')->insertGetId([
                    'appid' => $post['appid'],
                    'secret' => $post['secret'],
                    'merchantid' => $post['merchantid'],
                    'key' => $post['key'],
                    'remark' => $post['remark'],
                    'is_select' => $post['is_select'],
                ]);

                if ($db) {
                    $data = ['ret' => '添加成功'];
                } else {
                    $data = ['err' => '添加失败'];
                }
            } else {
                $update = [
                    'appid' => $post['appid'],
                    'merchantid' => $post['merchantid'],
                    'remark' => $post['remark'],
                    'is_select' => $post['is_select'],
                ];
                if ($post['secret']) {
                    $update['secret'] = $post['secret'];
                }
                if ($post['key']) {
                    $update['key'] = $post['key'];
                }
                $db = Db::name('config_wxpay')->where(['id' => $id])->update($update);

                if ($db) {
                    $data = ['ret' => '修改成功'];
                } else {
                    $data = ['err' => '修改失败'];
                }
            }
            if ($db && $post['is_select']) {
                Db::name('config_wxpay')->where(['id'=>['<>', $id]])->update(['is_select'=>0]);
            }
        } else {
            if ($id) {
                $name = Db::name('config_wxpay')->where('id', $id)->find();
                if ($name) {
                    $this->assign('name', $name);
                } else {
                    $data = ['err' => 'id参数错误'];
                }
            }
            $data = $this->fetch();
        }
        return $data;
    }
    public function del_wxpay()
    {
        if (request()->isAjax()) {
            $id = input('id');
            if ($id) {
                $db = Db::name('config_wxpay')->where(['id' => $id, 'is_select'=>0])->delete();
                if ($db) {
                    $data = ['ret' => '删除成功'];
                } else {
                    $data = ['err' => '删除失败'];
                }
            } else {
                $data = ['err' => 'id参数错误'];
            }
        } else {
            $data = ['err' => '提交参数错误'];
        }
        return $data;
    }
}
 